
define(['vue',
		'config',
		'common'
],function(Vue,config,utils){
	'use strict';

	var shopApplicationView = new Vue({
		el:'#app',
		data:{
			province:'',
			city:'',
			district:'',
		},
		created:function(){
			utils.userToken();
			
			var _this = this;
			
			utils.ajax(utils.getWebUrl('getBaseRegionAll'),null,'POST',function(data){
				//console.log(data)
				_this.province = data.data.province;  
				_this.city = data.data.city;  
				_this.district = data.data.district;

				$.each(_this.province, function (k, p) { 
	                var option = "<option value='" + p.id + "'>" + p.name + "</option>";
	                $("#selProvince").append(option);

	                var applyshops = utils.getData('applyshops');
	                if(applyshops){
	                	$('#shopsName').val(applyshops.shopsName);
			            $('#addressDetail').val(applyshops.shopsAddress);
			            $('#shopsLinkman').val(applyshops.shopsLinkman);
			            $('#shopsLinkphone').val(applyshops.shopsLinkphone);
		                
		                $($('#selProvince option')[applyshops.province]).attr('selected',true);
		                _this.selProvince($('#selProvince option')[applyshops.province],'i');
		                
		            }
	                
	            });
			},function(){
				$('.DataLoading').show();
			},function(){
				$('.DataLoading').hide();
			})     
		},
		methods:{
			backhome:function(){
				utils.setData('applyshops','');
				utils.setData('applyshopsPic','');
				window.location.href = "userInfo.html";
			},
			selProvince:function(event,i){
				var _this = this;
				var selValue;
				if(i === 'i'){
					selValue = $(event).val();
				}else{
					selValue = $(event.currentTarget).val();
				}
				
                $("#selCity option:gt(0)").remove();
                 
                $.each(_this.city, function (k, p) { 
                    if (p.parentId == selValue) {
                        var option = "<option value='" + p.id + "'>" + p.name + "</option>";
                        $("#selCity").append(option);
                    }
                });

                if(i === 'i'){
                	var applyshops = utils.getData('applyshops');
                	$($('#selCity option')[applyshops.city]).attr('selected',true);
		            _this.selCity($('#selCity option')[applyshops.city],'i');
                }
			},
			selCity:function(event,i){
				var _this = this;
				var selValue;
				if(i === 'i'){
					selValue = $(event).val();
				}else{
					selValue = $(event.currentTarget).val();
				}

                $("#selDistrict option:gt(0)").remove(); 

                $.each(_this.district, function (k, p) {
                    if (p.parentId == selValue) {
                        var option = "<option value='" + p.id + "'>" + p.name + "</option>";
                        $("#selDistrict").append(option);
                    }
                });

                if(i === 'i'){
                	var applyshops = utils.getData('applyshops');
	                if(applyshops.county ===''){
						$($('#selDistrict option')[applyshops.area]).attr('selected',true);
	                }else{
	                	$($('#selDistrict option')[applyshops.county]).attr('selected',true);
	                }
                }
                
			},
			toshopLicense:function(){
				var shopsName = $('#shopsName').val();
				var addressDetail = $('#addressDetail').val();
				var shopsLinkman = $('#shopsLinkman').val();
				var shopsLinkphone = $('#shopsLinkphone').val();

				var province = $("#selProvince option:selected");
				var city = $("#selCity option:selected");
				var district = $("#selDistrict option:selected");
				/*var address = $('#address').text();*/

				
				if($.trim(shopsName) === ""){
					utils.appAlert('商家名称不能为空');
                    return;
				}

				if($.trim(province[0].value) === "0"){
					utils.appAlert('请选择省份');
                    return;
				}

				if($.trim(city[0].value) === "0"){
					utils.appAlert('请选择城市');
                    return;
				}

				if($.trim(district[0].value) === "0"){
					utils.appAlert('请选择区/县');
                    return;
				}

				if($.trim(addressDetail) === ""){
					utils.appAlert('详细地址不能为空');
                    return;
				}

				if($.trim(shopsLinkman) === ""){
					utils.appAlert('负责人姓名不能为空');
                    return;
				}

				if($.trim(shopsLinkphone) === ""){
					utils.appAlert('负责人联系电话不能为空');
                    return;
				}

				if(!(/^1(3|4|5|7|8)\d{9}$/.test(shopsLinkphone))){
                    utils.appAlert('请输入正确的手机号码');
                    return;
				}

				//缺少验证的接口
				utils.ajax(utils.getWebUrl('shop/isShopp'),{
					phone: shopsLinkphone
				},'POST',function(res){
					if(res.msg == true){
						utils.appAlert('该手机已注册为商家',null,null,null,null,'提示');
						return;
					}


					var applyshops = new Object();

					applyshops.shopsName = shopsName;
			        applyshops.shopsAddress = addressDetail;
			        applyshops.shopsLinkman = shopsLinkman;
			        applyshops.shopsLinkphone = shopsLinkphone;

			        //地区
			        applyshops.province = province[0].index;
			        applyshops.city = city[0].index;
			        applyshops.provinceID = province[0].value;
			        applyshops.cityID = city[0].value;
			        
			        if(district[0].innerText.indexOf('县')){
			        	applyshops.county = district[0].index;
			        	applyshops.countyID = district[0].value;
			        }else{
			        	applyshops.area = district[0].index;
			        	applyshops.areaID = district[0].value;
			        }
			        		       
					utils.setData('applyshops',applyshops);
					window.location.href="shopApplication_License.html";
					
				})
			}
		}
	})

	return shopApplicationView
});